package com.lee.sshclient.plugin;

import com.lee.sshclient.ShellClient;
import org.apache.cordova.CallbackContext;

/**
 * Created by le on 2016/1/28.
 */
public class SSHExecuteRunnable implements Runnable {

    private final ShellClient shellClient;
    private final CallbackContext callbackContext;
    private final String command;

    public SSHExecuteRunnable(ShellClient shellClient, CallbackContext callbackContext, String command) {
        this.shellClient = shellClient;
        this.callbackContext = callbackContext;
        this.command = command;
    }

    @Override
    public void run() {
        try {
            shellClient.execShell(command + (command.endsWith("\n") ? "" : "\n"));
            callbackContext.success();
        } catch (Exception e) {
            e.printStackTrace();
            callbackContext.error(e.getMessage());
            // 断开链接
            shellClient.disconnect();
        }
    }

}
